#!/bin/sh -ex

echo "This one needs adaption..."
sleep 10

VERSION="fiasco-1.2"
CURRDIR=$(pwd)
SCRATCH="$CURRDIR/release-build"
WORKDIR="$SCRATCH/$VERSION"
EXCLUDE="$SCRATCH/exclude.$$"
KBLDDIR="$WORKDIR/l4/kernel/fiasco/build"

CVSHOME="/home/cvs"
KERNPKG="l4/kernel/fiasco"
USERPKG="$(echo	l4/COPYING l4/LICENSING l4/Makefile							\
		l4/tool/preprocess/src/preprocess)"

umask 022

trap "rm -rf $EXCLUDE && exit 1" 1 2 15
trap "rm -rf $EXCLUDE && exit 0" 0

if [ -d "$SCRATCH" ];
then
  echo "$SCRATCH exists, please remove it before calling $0."
  exit 1
fi

# Create a temporary working directory
mkdir -p $SCRATCH
mkdir -p $WORKDIR

# Create file blacklist
echo '*CVS*'       > $EXCLUDE
echo '.cvsignore' >> $EXCLUDE

# Check out Userland stuff
(cd $WORKDIR && cvs -d $CVSHOME co $USERPKG)

# Create Userland archive
(cd $SCRATCH && tar -X $EXCLUDE -cvpf $VERSION.tar $VERSION/l4)

# Check out Kernel stuff
(cd $WORKDIR && cvs -d $CVSHOME co $KERNPKG)

# mv README and CHANGES down to root of the archive so that users have
# something to read when they cd into fiasco-*
mv $WORKDIR/l4/kernel/fiasco/README $WORKDIR/l4/kernel/fiasco/CHANGES $WORKDIR
(cd $SCRATCH && tar -rvpf $VERSION.tar $VERSION/README $VERSION/CHANGES )

# Add kernel to archive
(cd $SCRATCH && tar -X $EXCLUDE -rvpf $VERSION.tar $VERSION/l4/kernel/fiasco)

echo "Compressing tar files..."
( cd $SCRATCH
  # *zip the dang thing
  gzip  -c9 $VERSION.tar        > $VERSION.tar.gz
  bzip2 -9  $VERSION.tar

  # Checksums for everyone
  md5sum $VERSION.tar.gz        >> $VERSION.tar.gz.md5
  md5sum $VERSION.tar.bz2       >> $VERSION.tar.bz2.md5
)

# Clean up
rm -rf $WORKDIR
rm -rf $KBLDDIR

echo Done!
